package com.googlecode.designrules;

import java.net.URL;

class ClassPath {

	private static final String CLASS_FILE = ".class";

	public static String from(Class<?> target) {
		String classnameAsPath = '/' + target.getName().replace('.', '/') + CLASS_FILE;

		URL resource = ClassPath.class.getResource(classnameAsPath);
		String protocol = resource.getProtocol();
		String file = resource.getFile();
		if ("file".equals(protocol)) {
			return file.substring(0, file.indexOf(classnameAsPath));

		} else if ("jar".equals(protocol)) {
			String jarFilename = file.substring(0, file.indexOf('!'));
			return jarFilename.substring("file:".length());

		} else {
			throw new RuntimeException("Unresolved protocol: " + protocol);
		}
	}

} 